package com.wyp168.algorithmStudy;

import java.util.concurrent.Semaphore;

/**
 * @ClassName selectSortByTwoFor
 * @Description TODO
 * @Author wyp168
 * @Date 2022/9/29 22:46
 */
public class SelectSortByTwoFor {
    public static void main(String[] args) {
        int[] nums = {4,2,5,1,6};
        SelectSortByTwoFor selectSortByTwoFor = new SelectSortByTwoFor();
        int[] ints = selectSortByTwoFor.selectSort(nums);
        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }

    public int[] selectSort(int[] nums) {
        if (nums.length < 2) {
            return nums;
        }
        for (int i = 0; i < nums.length - 1; i++) {
            int curMinIndex = i;

            for (int j = i + 1; j < nums.length - 1; j++) {
                if (nums[j] < nums[curMinIndex]) {
                    //找到最小的元素下表，并记录
                    curMinIndex = j;
                }
            }

            if (curMinIndex != i) swap(nums, i, curMinIndex);
        }

        return nums;
    }

    public void swap (int[] nums, int newIndex, int curMinIndex) {
        int temp = nums[newIndex];
        nums[newIndex] = nums[curMinIndex];
        nums[curMinIndex]= temp;
    }
}
